<template>
  <div>
    <div class="container">
      <!--标题-->
      <div class="title">{{ video.title }}</div>
      <!--视频数据-->
      <div class="video-data">
        <div class="view-item">
          <i class="iconfont icon-tuya-"></i>
          <span>{{ video.views | numberFilter }}</span>
        </div>
        <div class="view-item">
          <i class="iconfont icon-shijian"></i>
          <span>{{ video.createdAt | dateFormat }}</span>
        </div>
      </div>
      <!--播放器-->
      <div class="player">
        <d-player v-if="video" ref="player" :options="dplayerOptions"></d-player>
      </div>
      <!--点赞区-->
      <div class="video-toolbar-v1">
        <div class="toolbar-left">
          <span class="active" @click="publishVideoLike">
            <i :class="['iconfont',videoLiked(video.likedUserList)?'icon-dianzan_kuai':'icon-31dianzan']"></i>
                 <span class="info-text">
                {{ video.likes | numberFilter }}
               </span>
          </span>
          <span class="active" @click="publishVideoEnshrine">
            <i :class="['iconfont',hasEnshrined(video.enshrinedUserList)? 'icon-shoucang':'icon-shoucang1']"></i>
                 <span class="info-text">
              {{ video.enshrine | numberFilter }}
               </span>
          </span>
        </div>
      </div>
      <!--评论区-->
      <div class="comment">

        <div class="bb-comment">
          <!--发送头部信息-->
          <div class="comment-header">
            <div class="tabs-order">
              <div class="default-sort">评论</div>
              <div class="total-reply">{{ pageInfo.total |numberFilter }}</div>
            </div>
          </div>
          <!--发送框-->
          <div class="comment-send">
            <!--头像-->
            <div class="user-face">
              <div class="bili-avatar">
                <img alt="" :src="avatarSrc"/>
              </div>
            </div>
            <!--输入框-->
            <div class="textarea-container">
              <el-input
                type="textarea"
                v-if="isLogin"
                class="submit-textarea"
                :rows="5"
                placeholder="发一条友善的弹幕"
                :disabled="!isLogin"
                v-model="commitForm.content">
              </el-input>
              <div class="unLoginContainer" v-if="!isLogin">
                请先登录后发表评论
                <el-button type="primary" size="mini" @click="maskOn">登录</el-button>
              </div>
            </div>
            <div class="comment-submit" @click="submitComment">
              <span class="submit-text">发布</span>
            </div>
          </div>
          <!--评论列表-->
          <div class="comment-list">
            <div class="reply-wrap infinite-list-item" v-for="comment in commentList" :key="comment.id">
              <div class="user-face">
                <div class="bili-avatar">
                  <img :src="avatarUrl" alt="">
                </div>
              </div>
              <div class="con">
                <div class="user">
                  <span>{{ comment.userName }}</span>
                </div>
                <p class="text">{{ comment.content }}
                </p>
                <div class="info">
                  <div class="time">{{ comment.createdAt | dateFormat }}</div>
                  <div class="like cursor" @click="publishLikes(comment)">
                    <i :class="['iconfont',!hasLiked(comment)?'icon-31dianzan':'icon-dianzan_kuai']"></i>
                    <span>{{ comment.likes | numberFilter }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!--分页器-->
          <div class="comment-pagination" v-if="pageInfo.total>5">
            <el-pagination
              background
              layout="prev, pager, next"
              @current-change="handleCurrentChange"
              :page-size="pageInfo.pageSize"
              :current-page.sync="pageInfo.pageNum"
              :total="pageInfo.total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import videoApi from "@/api/video";
import {mapState} from "vuex";
import commentApi from "@/api/comment";

export default {
  name: "Details",
  data() {
    return {
      baseUrl: "http://localhost:8963",
      /*视频Id*/
      videoId: "",
      /*视频信息*/
      video: {},
      /*添加评论表单*/
      commitForm: {
        content: ''
      },
      /*评论列表*/
      commentList: [],
      /*分页参数*/
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
        total: 0
      }
    };
  },
  computed: {
    //player配置
    dplayerOptions() {
      return {
        theme: '#b7daff',  // 风格颜色，例如播放条，音量条的颜色
        loop: false,  // 是否自动循环
        lang: 'zh-cn',  // 语言，'en', 'zh-cn', 'zh-tw'
        screenshot: false,  // 是否允许截图（按钮），点击可以自动将截图下载到本地
        hotkey: true,  // 是否支持热键，调节音量，播放，暂停等
        volume: 0.7,  // 初始化音量
        autoplay: false,
        video: {
          url: ""
        },
        currentImgSrc: ''
      };
    },
    ...mapState({
      avatarSrc: state => state.user.avatarSrc,
      isLogin: state => state.user.isLogin,
      userId: state => state.user.userInfo.userId
    }),
    avatarUrl: {
      cache: true,
      get() {
        let api = "https://api.multiavatar.com/";
        return api + (Math.random() * 10000) + ".svg?apikey=ox4mpMeVQP9fHY";
      }
      ,
    }
  },
  methods: {
    //保存视频地址信息
    async saveAndGetVideoPathToSession() {
      if (this.$route.params.videoId === undefined) {
        let sessionPath = sessionStorage.getItem("videoId");
        sessionPath ? this.videoId = sessionPath : this.$router.push("/");
      } else {
        this.videoId = this.$route.params.videoId;
        sessionStorage.setItem("videoId", this.$route.params.videoId);
      }
      let res = await videoApi.getVideoById(this.videoId);
      this.video = res.data.video;
      let url = this.baseUrl + "/play/" + this.video.videoPath;
      this.switchVideo(url);
      await this.getCommentList();
    },
    /*切换视频*/
    switchVideo(url, pic) {
      let dp = this.$refs.player.dp;
      dp.switchVideo({
        url,
        pic: pic || ""
      });
      dp.play();
    },
    /*登录*/
    maskOn() {
      this.$bus.$emit("maskShow");
    },
    /*获取评论列表*/
    async getCommentList() {
      let params = {videoId: this.video.id, ...this.pageInfo};
      let result = await commentApi.getCommentList(params);
      this.commentList = result.data.results;
      this.pageInfo.total = result.data.total;
    },
    /*发表评论*/
    async submitComment() {
      let commitForm = {...this.commitForm};
      this.commitForm.content = "";
      if (commitForm.content?.length > 0) {
        commitForm.userId = this.userId;
        commitForm.videoId = this.videoId;
        await commentApi.publishComment(commitForm);
        this.$modal.msgSuccess("发布成功");
        await this.getCommentList();
      } else {
        this.$modal.msgWarning("请输入内容发再发布");
      }
    },
    /*分页器*/
    handleCurrentChange(pageNum) {
      this.pageInfo.pageNum = pageNum;
      this.getCommentList();
    },
    /*判断是否已经点赞*/
    hasLiked({likedUserList}) {
      return likedUserList.includes(this.userId);
    },
    videoLiked(likedUserList) {
      if (likedUserList) return likedUserList.includes(this.userId);
    },
    /*判断是否已经收藏*/
    hasEnshrined(enshrinedUserList) {
      if (enshrinedUserList) return enshrinedUserList.includes(this.userId);
    },
    /*评论点赞*/
    async publishLikes(comment) {
      let {id, likes, likedUserList} = comment;
      let userId = this.userId;
      let dataForm = {
        id,
        likes,
        userId,
      };
      await commentApi.publishLikes(dataForm);
      if (likedUserList.includes(userId)) {
        comment.likedUserList = likedUserList.filter(item => item !== userId);
        comment.likes -= 1;
      } else {
        comment.likedUserList.push(userId);
        comment.likes += 1;
      }
    },
    /*视频点赞*/
    async publishVideoLike() {
      let {likes, id, likedUserList} = this.video;
      let userId = this.userId;
      await videoApi.publishVideoLike({likes, id, userId});
      if (likedUserList.includes(userId)) {
        this.video.likedUserList = likedUserList.filter(item => item !== userId);
        this.video.likes -= 1;
      } else {
        this.video.likedUserList.push(userId);
        this.video.likes += 1;
      }
    },
    /*视频收藏*/
    async publishVideoEnshrine() {
      let {enshrine, id, enshrinedUserList} = this.video;
      let userId = this.userId;
      await videoApi.publishVideoEnshrine({enshrine, id, userId});
      if (enshrinedUserList.includes(userId)) {
        this.video.enshrinedUserList = enshrinedUserList.filter(item => item !== userId);
        this.video.enshrine -= 1;
      } else {
        this.video.enshrinedUserList.push(userId);
        this.video.enshrine += 1;
      }
    }
  },
  created() {
    this.saveAndGetVideoPathToSession();

  },
  beforeDestroy() {
    sessionStorage.removeItem("videoId");
  }
};
</script>

<style scoped lang="less">
.container {
  max-width: 2540px;
  min-width: 1080px;
  width: 1080px;
  margin: 0 auto;
  box-sizing: content-box;
  overflow: auto;

  .title {
    margin-top: 20px;
    font-size: 20px;
    font-family: PingFang SC, HarmonyOS_Regular, Helvetica Neue, Microsoft YaHei, sans-serif;
    font-weight: 500;
    -webkit-font-smoothing: antialiased;
    color: #18191C;
    line-height: 24px;
    margin-bottom: 6px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .video-data {
    font-size: 13px;
    color: #9499A0;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: flex;
    align-items: center;
    height: 24px;
    line-height: 18px;

    .view-item {
      display: inline-flex;
      align-items: center;
      margin-right: 12px;

      .iconfont {
        margin-right: 4px;
        width: 20px;
        height: 20px;
        font-size: 20px;
      }
    }
  }

  .player {
    margin-top: 20px;
    height: 422px;

    .dplayer {
      height: 100%;
      border-radius: 10px;

      ::v-deep .dplayer-video {
        width: 100%;
        height: 100%;
      }
    }
  }

  .video-toolbar-v1 {
    box-sizing: content-box;
    border-bottom: 1px solid #e3e5e7;
    padding-top: 16px;
    padding-bottom: 12px;
    height: 28px;
    line-height: 28px;
    display: flex;

    .toolbar-left {
      float: left;
      position: relative;
      user-select: none;
      font-size: 13px;
      font-family: PingFang SC, HarmonyOS_Regular, Helvetica Neue, Microsoft YaHei, sans-serif;
      font-weight: 500;
      display: flex;
      align-items: center;
      height: 100%;


      span {
        width: 92px;
        margin-right: 8px;
        cursor: pointer;
        transition: all 0.3s;
        white-space: nowrap;
        position: relative;
        display: inline-flex;
        align-items: center;

        &:hover {
          color: #74bffe
        }

        .iconfont {
          font-size: 23px;
          margin-right: 8px;
        }

        .icon-dianzan_kuai {
          color: #00AEEC;
        }

        .icon-shoucang {
          color: #00AEEC;
        }

        .info-text {
          overflow: hidden;
          text-overflow: ellipsis;
          word-break: break-word;
          white-space: nowrap;
        }
      }
    }

  }

  .comment {
    position: relative;
    margin-top: 24px;

    .bb-comment {
      font-family: PingFang SC, HarmonyOS_Regular, Helvetica Neue, Microsoft YaHei, sans-serif;
      font-weight: 400;
      min-height: 100px;
      background: #ffffff;
      font-size: 0;
      // 发送评论头部
      .comment-header {
        margin: 0 0 22px 0;

        .tabs-order {
          display: flex;
          align-items: center;

          .default-sort {
            height: 34px;
            margin-right: 6px;
            line-height: 34px;
            color: #333333;
            font-weight: 500;
            font-size: 20px;
          }

          .total-reply {
            height: 20px;
            margin-right: 36px;
            line-height: 20px;
            pointer-events: none;
            font-size: 13px;
            border: 0;
            border-radius: 0;
            background-color: transparent;
            cursor: pointer;
            vertical-align: middle;
            color: #61666d;
          }
        }
      }

      // 发送评论区域
      .comment-send {
        margin-bottom: calc(40px * 0.35);
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        position: relative;
        font-size: 14px;
        /*头像*/

        .user-face {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 50px;
          /*图片*/

          .bili-avatar {
            width: 40px;
            height: 40px;
            display: block;
            position: relative;
            background-size: cover;
            border-radius: 50%;
            margin: 0;
            padding: 0;
          }
        }

        /*输入框*/

        .textarea-container {
          margin-left: 10px;
          margin-right: 30px;
          flex-grow: 1;
          position: relative;

          .unLoginContainer {
            height: 50px;
            width: 100%;
            font-size: 12px;
            border-radius: 4px;
            text-align: center;
            color: #9499a0;
            background-color: #f1f2f3;
            overflow: hidden;
            line-height: 50px;
          }

          .submit-textarea {
            /deep/ textarea {
              font-size: 13px;
              display: inline-block;
              box-sizing: border-box;
              background-color: #f1f2f3;
              border: 1px solid #f1f2f3;
              overflow: auto;
              border-radius: 6px;
              color: #18191c;
              width: 100%;
              height: 50px;
              transition: 0.2s;
              padding: 5px 10px;
              outline: none;
              vertical-align: middle;
              resize: none;
              line-height: 38px;

              &:focus-visible {
                height: 65px !important;
                background-color: #ffffff;
                border: 1px solid #c9ccd0;
                line-height: normal;
              }
            }
          }
        }

        .comment-submit {
          width: 70px;
          height: 50px;
          padding: 4px 15px;
          font-size: 16px;
          background-color: #00AEEC;
          color: #ffffff;
          border-radius: 4px;
          text-align: center;
          vertical-align: top;
          cursor: pointer;
          border: none;
          transition: 0.1s;
          outline: none;
          box-sizing: border-box;
          line-height: 42px;
          margin-right: 300px;
        }
      }

      // 评论列表
      .comment-list {
        box-sizing: content-box;

        .reply-wrap {
          display: flex;
          /*头像*/

          &:first-child .con {
            border-top: none;
          }

          .user-face {
            width: calc(1.7 * 40px);

            .bili-avatar {
              width: 60px;
              height: 60px;
              margin-right: auto;
              display: flex;
              align-items: center;
              margin-top: 5px;

              img {
                width: 34px;
                height: 34px;
              }
            }
          }

          /*内容*/

          .con {
            padding: 22px 0 14px 0;
            border-top: 1px solid #e3e5e7;
            width: 100%;

            .user {
              display: flex;
              align-items: center;
              margin-bottom: 2px;

              span {
                color: #FB7299;
                vertical-align: bottom;
                text-decoration: none;
                cursor: pointer;
                font-size: 10px;
              }
            }

            .text {
              margin-top: 5px;
              z-index: 2;
              font-size: 14px;
              line-height: 24px;
              text-shadow: none;
              overflow: hidden;
              word-wrap: break-word;
              word-break: break-word;
              white-space: pre-wrap;
              margin-bottom: 5px;
              color: #18191c;
            }

            .info {
              font-size: 13px;
              line-height: 18px;
              display: flex;
              flex-wrap: wrap;
              position: relative;
              align-items: center;
              color: #9499a0;
              margin-bottom: 0;

              .time {
                margin-right: 19px;
              }

              .like {
                .icon-31dianzan {
                  font-size: 16px;
                  margin-right: 5px;
                }

                .icon-dianzan_kuai {
                  font-size: 16px;
                  margin-right: 5px;
                  color: #00AEEC;
                }

                &:hover {
                  .icon-31dianzan {
                    color: #00AEEC;
                  }
                }
              }
            }
          }
        }
      }

      //分页器
      .comment-pagination {
        margin: 20px 0;
        display: flex;
        justify-content: center;
      }
    }
  }

}
</style>
